<template>
    <el-container>
    <el-header height="80px">
        <el-row class="header">
            <el-col :span="4"><h1>XXX管理系统:</h1></el-col>
            <el-col :span="4" :offset="15"><h3>用户名：{{sessionUser.data.name}}</h3></el-col>
            <el-col :span="1" ><el-button type="primary" circle @click="quite">退出</el-button></el-col>
        </el-row>
        
    </el-header>
    <el-container>
        <el-aside width="200px">
            <el-menu
      :default-active="$router.history.current.path"
      class="el-menu-vertical-demo"

      background-color="#545c64"
      text-color="#fff"
      active-text-color="#ffd04b"
      :router="true">
      <template>

      </template>
      <!-- <el-menu-item v-for="ele in functions" :key="ele.functionId" :index="ele.url">
        <i class="el-icon-menu"></i>
        <span slot="title">{{ele.functionName}}</span>
      </el-menu-item> -->
      <el-menu-item index="/menu/index">
        <i class="el-icon-menu"></i>
        <span slot="title">用户管理</span>
      </el-menu-item>
      <el-menu-item index="/manage/student">
        <i class="el-icon-menu"></i>
        <span slot="title">学生管理</span>
      </el-menu-item>
      <el-menu-item index="/manage/student">
        <i class="el-icon-menu"></i>
        <span slot="title">班级管理</span>
      </el-menu-item>
      <el-menu-item index="/manage/student">
        <i class="el-icon-menu"></i>
        <span slot="title">课程管理</span>
      </el-menu-item>
      
    </el-menu>
        </el-aside>
        <el-main>
            <router-view></router-view>
        </el-main>
    </el-container>
</el-container>
</template>

<script>
import {mapState,mapMutations,mapActions} from "vuex"
export default {
  computed:{
        ...mapState("user",["loginUser","sessionObj"]),
    },
    data(){
      return{
        sessionUser:{}
      }
    },
    methods:{
      ...mapMutations("user",["setLoginUser","setSessionObj"]),
      // ...mapActions('user',["getLoginUser"]),
     async quite(){
        // var info = await this.ajax.getSubmit("/project/users/cleanUser");
        this.$cookies.remove("token");
        
        this.$router.push("/login");
        
      },
       async me(){//得到登陆用户对象 
            // 获取路径后面的token信息，如果没有则从cookie里面获取
            let token = this.$router.history.current.query.token;
            if(!token){
              token = this.$cookies.get('token')
            }else{
              this.$cookies.set('token',token)
            }
                    // console.log(token);
            var userObj = await this.ajax.postSubmitToken("/project/users/token",null,token); 
            // alert(JSON.stringify(userObj) + "***");
            this.sessionUser = userObj;
            // alert(JSON.stringify(this.sessionUser));

            if(userObj.status == 2009){
            // 刷新token后将新的token保存到cookie中
            // console.log("refreshToken",this.$cookies.get('refreshToken'));
            let newTokenResponse = await this.ajax.getSubmit("/project/users/refreshToken",null,this.$cookies.get('refreshToken'));
            this.$cookies.set("token",newTokenResponse.data);
             this.sessionUser = await  this.ajax.postSubmitToken("/project/users/token",null,token); 
          }
        },
      async checkSessionUser(){      
        await this.me();       
        if(this.sessionUser.data != null){
          
        }else{
          this.$router.push("/login");
        }
      }
    },
    created(){
      this.checkSessionUser();
    }

}
</script>

<style>

</style>